﻿/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;

namespace DaveSexton.DocProject.Sandcastle.TopicManagement
{
	public class TopicMetadata
	{
		#region Public Properties
		public bool IsDirty { get; set; }

		public string TopicId
		{
			get
			{
				return topicId;
			}
		}

		public string Title
		{
			get
			{
				return title;
			}
			set
			{
				if (!string.Equals(title, value, StringComparison.Ordinal))
				{
					title = value;
					NotifyPropertyChanged();
				}
			}
		}

		public string TocTitle
		{
			get
			{
				return tocTitle;
			}
			set
			{
				if (!string.Equals(tocTitle, value, StringComparison.Ordinal))
				{
					tocTitle = value;
					NotifyPropertyChanged();
				}
			}
		}

		public KeywordMultiCollection Keywords
		{
			get
			{
				return keywords;
			}
		}

		public MetadataCollection Attributes
		{
			get
			{
				return attributes;
			}
		}
		#endregion

		#region Private / Protected
		private readonly string topicId;
		private readonly KeywordMultiCollection keywords;
		private readonly MetadataCollection attributes;
		private string title, tocTitle;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="TopicMetadata" /> class.
		/// </summary>
		public TopicMetadata(string topicId)
		{
			if (string.IsNullOrEmpty(topicId))
				throw new ArgumentException(Resources.Errors.NullOrEmpty, "topicId");

			this.topicId = topicId;
			this.keywords = new KeywordMultiCollection();
			this.attributes = new MetadataCollection();

			this.keywords.Changed += (s, e) => NotifyPropertyChanged();
			this.attributes.Changed += (s, e) => NotifyPropertyChanged();
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="TopicMetadata" /> class.
		/// </summary>
		public TopicMetadata(string topicId, string title, string tocTitle, KeywordMultiCollection keywords, MetadataCollection attributes)
		{
			if (string.IsNullOrEmpty(topicId))
				throw new ArgumentException(Resources.Errors.NullOrEmpty, "topicId");

			this.topicId = topicId;
			this.title = title;
			this.tocTitle = tocTitle;
			this.keywords = keywords ?? new KeywordMultiCollection();
			this.attributes = attributes ?? new MetadataCollection();

			this.keywords.Changed += (s, e) => NotifyPropertyChanged();
			this.attributes.Changed += (s, e) => NotifyPropertyChanged();
		}
		#endregion

		#region Methods
		private void NotifyPropertyChanged()
		{
			IsDirty = true;
			OnPropertyChanged(EventArgs.Empty);
		}
		#endregion

		#region Events
		private readonly object PropertyChangedEventLock = new object();
		private EventHandler PropertyChangedEvent;

		/// <summary>
		/// Event raised when a property value changes.
		/// </summary>
		public event EventHandler PropertyChanged
		{
			add
			{
				lock (PropertyChangedEventLock)
				{
					PropertyChangedEvent += value;
				}
			}
			remove
			{
				lock (PropertyChangedEventLock)
				{
					PropertyChangedEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="PropertyChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="EventArgs" /> object that provides the arguments for the event.</param>
		private void OnPropertyChanged(EventArgs e)
		{
			EventHandler handler = null;

			lock (PropertyChangedEventLock)
			{
				handler = PropertyChangedEvent;
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion
	}
}
